<?php

namespace App\Http\Controllers\AdminShop\Shoper;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;

class RegisterController extends Controller
{
    /**
     * 用户注册
     */
    public function index()
    {
        return view('adminShop.shoper.register');
    }

    /**
     * 用户执行注册
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            // 'phone' => 'required|unique:shopers|regex:/^1[34578][0-9]{9}$/',
            'phone' => 'required|unique:shopers|regex:/^1\d{10}$/',
            'shopername' => 'required',
            'password' => 'required|regex:/^\w{3,12}$/|same:repassword'
        ],[
            'phone.required' => '手机号未填写',
            'phone.unique' => '手机号已被注册，请更好手机号',
            'phone.regex' => '手机号格式不正确',
            'shopername.required' => '店家名称必填',
            'password.required' => '密码未填写',
            'password.regex' => '密码格式不正确',
            'password.same' => '俩次密码不一致'
        ]);

        $data = $request->except(['_token','repassword']);
        $data['password'] = Hash::make($data['password']);

        // 事务处理
        DB::beginTransaction();
        $id = DB::table('shopers')->insertGetId($data);
        // 获取插入数据的id
        $shops['spid'] = $id;
        $res = DB::table('shops')->insert($shops);

        if($id && $res) {
            DB::commit();
            return redirect('/adminShop/shoperLogin')->with('info','注册成功,请登陆');
        }else{
            DB::rollback();
            return back()->with('info','注册失败');
        }

    }

    
}
